import { ErrorRequestHandler, NextFunction, RequestHandler } from "express";
import { CusstomRequestHandler } from "./overried/express";
import { APIError } from "./model/shared/APIErroy";

export const authenticator: CusstomRequestHandler = (req, res, next) => {
  let username = "keqi";
  req.user = username;
  next();
}
export const logger: CusstomRequestHandler = (req, res, next) => {
  console.log( "user:"+req.user)
  console.log(new Date() + "-" + req.method + "-" + "request to" + req.path);
  next();
}
export const cc:CusstomRequestHandler=(req,res,next)=>{
  res.cc = function (err, status = 1) {
    // 在这里编写发送响应数据的逻辑
      res.send({
        // 状态
        status,
        // 状态描述，判断 err 是 错误对象 还是 字符串
        message: err instanceof Error ? err.message : err,
      })
  };
}
export const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
  console.log("find error");
  return res.status(err.status ? err.status : 500).json({ status: err.status, message: err.name + ":" + err.message });
}

